#pragma once

#include <set>
#include "event_loop.h"
#include "thread/event_loop_thread_pool.h"
#include "net_callback.h"

namespace rr {
namespace nio {

class TcpServer : public std::enable_shared_from_this<TcpServer>
{
public:
    using Sptr = std::shared_ptr<TcpServer>;

public:
    TcpServer(EventLoop::Sptr loop, const EventLoopThreadPool::Sptr thread_pool = nullptr);
    ~TcpServer();

    void SetConnectionCallback(const ConnectionCallback& cb) { connection_cb_ = cb; }

    int32_t Start(int32_t backlog = 1024);
    int32_t Stop();

private:
    void DoAccept();
    void OnAccept(const boost::system::error_code &ec, boost::asio::ip::tcp::socket& socket, EventLoop::Sptr loop);
    EventLoop::Sptr GetWorkLoop();

private:
    EventLoop::Sptr loop_;
    EventLoopThreadPool::Sptr work_thread_pool_;
    boost::asio::ip::tcp::acceptor acceptor_;
    ConnectionCallback connection_cb_ = nullptr;
};


} //namespace 
} //namespace 